package com.dc.event.listener.handler;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.pentaho.di.job.Job;

import com.dc.di.util.DateService;
import com.dc.di.xml.XmlStringBuilder;

public abstract class AbstractHandler implements JobStatusHandler {
	protected static Logger logger = Logger.getLogger(AbstractHandler.class);
	protected static Properties p;
	static {
		InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("./etc/event/target.properties");
            p = new Properties();
            p.load(inputStream);
        }catch (IOException e) {
        	throw new RuntimeException(e);
        }finally{
        	if(inputStream != null){
        		try {
        			inputStream.close();
				} catch (Exception e) {}        		
        	}
        }
    }
	
	@Override
	public void reciveJobStatusInfo(Job job,
			List<Map<String, String>> jobStatus) {
	}
	
	protected XmlStringBuilder getXml(Job job,
			List<Map<String, String>> jobStatus) {
		XmlStringBuilder xml = new XmlStringBuilder(true);
		xml.appendHead("root");
		xml.append("jobname", job.getJobname());
		xml.append("jobInstanceId", job.getInstanceId());
		xml.append("startDate", DateService.format("yyyy-MM-dd HH:mm:ss", job.getStartDate()));
		xml.append("endDate", DateService.format("yyyy-MM-dd HH:mm:ss", job.getEndDate()));
		xml.append("status", job.getStatus());
		xml.append("errors", String.valueOf(job.getErrors()));
		xml.append("log", "<![CDATA["+job.getResult().getLogText()+"]]>");
		if(jobStatus != null){
			for (Map<String, String> map : jobStatus) {
				xml.appendHead("step");
					for(String key:map.keySet()){
						xml.append(key, map.get(key));
					}
				xml.appendTail("step");
			}
		}
		xml.appendTail("root");
		return xml;
	}
}
